import Vue from 'vue'
import VueRouter from "vue-router";
Vue.use(VueRouter);
// 引入路由组件
// 使用的路由懒加载

import store from '@/store';
const router = new VueRouter({
    routes: [{
        path: "",
        redirect: "/Home"
    },
    {
        path: "/Login",
        // 路由懒加载
        component: () => import('@/pages/Login/LoginPage.vue'),
        meta: {
            show: false
        }
    },
    {
        path: "/Home",
        component: () => import('@/pages/Home/HomePage.vue'),
        meta: {
            show: true
        }
    },
    {
        path: "/Search/:keyword?",
        name: 'Search',
        component: () => import('@/pages/Search/SearchPage'),
        meta: {
            show: true
        }
    },
    {
        path: "/Register",
        component:  () => import('@/pages/Register/RegisterPage'),
        meta: {
            show: false
        }
    },
    {
        path: "/Detail/:skuid?",
        component:  () => import('@/pages/Detail/DetailPage'),
        meta: {
            show: false
        },
    },
    {
        path: "/AddCartSuccess",
        name: "AddCartSuccess",
        component:  () => import('@/pages/AddCartSuccess/AddCartSuccess'),
        meta: {
            show: false
        },
    },
    {
        path: "/ShopCart",
        name: "ShopCart",
        component:  () => import('@/pages/ShopCart/ShopCart'),
        meta: {
            show: false
        },
    }, {
        // 待支付订单
        path: "/Trade",
        name: "Trade",
        component:  () => import('@/pages/Trade/Trade'),
        meta: {
            show: false
        },
        // 路由独享首位
        beforeEnter: (async (to, from, next) => {
            console.log(to);
            console.log(from);
            if (from.path === "/ShopCart") {
                next()
            } else {
                next(false)
            }
        }),
    },
    {
        // 待支付
        path: "/Pay",
        name: "Pay",
        component: () => import('@/pages/Pay/Pay'),
        meta: {
            show: false
        },
        // 路由独享首位
        beforeEnter: (async (to, from, next) => {
            if (from.path === "/Trade") {
                next()
            } else {
                next(false)
            }
        }),
    }, {
        // 支付成功
        path: "/PaySuccess",
        name: "PaySuccess",
        component:  () => import('@/pages/PaySuccess/PaySuccess'),
        meta: {
            show: false
        },
        // 路由独享首位
        beforeEnter: (async (to, from, next) => {
            if (from.path === "/Pay") {
                next()
            } else {
                next(false)
            }
        }),
    }, {
        // 我的订单
        path: "/Center",
        name: "Center",
        component:  () => import('@/pages/Center/Center'),
        meta: {
            show: false
        },
        children: [{
            path: "muOrder",
            component:  () => import('@/pages/Center/muOrder/muOrder'),
        }, {
            path: "groupOrder",
            component:  () => import('@/pages/Center/groupOrder/groupOrder'),
        }, {
            path: "/Center",
            redirect: '/Center/muOrder'
        }]
    },

    ],
    // vuerouter滚动行为,每次加载时回到顶部
    scrollBehavior(to, from, savedPosition) {
        // 始终滚动到顶部
        return {
            y: 0
        }
    },
});
router.beforeEach(async (to, from, next) => {
    //to:去的那个路由的信息
    //from:从那个路由而来的信息
    //next:放行函数!!!!!! 
    let userinfo = store.state.user.name
    let token = localStorage.getItem('token')
    // 如果有token进来
    if (token) {
        // 判断当前路由如果是login 你进来
        if (to.path == '/Login') {
            next('/')
        } else {
            // 一上来 userinfo是空值 因为没有派发
            if (userinfo) {
                next()
            } else {

                // 不是login就派发获取用户信息，并在任何地方都能用
                try {
                    await store.dispatch('disreqgetuserlist')
                    next()
                } catch (error) {
                    localStorage.removeItem('token')
                    next('/login')
                }
            }
        }
    } else {
        let toPath = to.path;
        if (toPath.indexOf('Trade') != -1 || toPath.indexOf('Pay') != -1 || toPath.indexOf('Renter') != -1 || toPath.indexOf("Center") != -1) {
            next('/login?redirect=' + toPath)
        } else {
            next()
        }

    }
})


// 解决vue编程式导航多次点击抛出的错误
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
    return VueRouterPush.call(this, to).catch(err => err)
}
export default router